package gui.feeds;

import gui.GUIUtils;
import gui.utilities.DateDiffCalculator;
import gui.utilities.Rater;

import java.awt.Dimension;

import javax.swing.GroupLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import core.objects.NewsFeedItem;
import db.threads.DBAccessThread;
import db.threads.IDBAccessThread;

public abstract class JFeedComment extends JPanel {

	private static final long serialVersionUID = 1L;
    public static final int OPEN_PANEL_HEIGHT = 400;
    public static final int CLOSE_PANEL_HEIGHT = 100;

	protected NewsFeedItem item;
	
	protected abstract IDBAccessThread getImageLoaderThread(JLabel imageLabel);
	protected abstract String getCommentedOn();
	protected abstract void loadPlaceHoldPicture(JLabel imageLabel);
	protected abstract String getAction();
	
	public JFeedComment(NewsFeedItem item) {
		this.item = item;
        initComponents();
        setMaximumHeight(CLOSE_PANEL_HEIGHT);
	}
	
    private void setMaximumHeight(int height) {
        setMaximumSize(new Dimension(Integer.MAX_VALUE, height));
        setPreferredSize(new Dimension(getWidth(), height));
    }

    private void initComponents() {
    	
    	setBorder(new EmptyBorder(10, 10, 10, 10));
    	
    	jPanel = new JPanel();
        jLabelImage = new javax.swing.JLabel();
        jLabelUserName = new javax.swing.JLabel();
        jPanelRating = new javax.swing.JPanel();
        jLabelComment = new javax.swing.JLabel();
        
        setBackground(new java.awt.Color(51, 153, 255));
        setAlignmentX(0.0F);
        setAlignmentY(0.0F);
        setOpaque(false);
        
        jLabelImage.setAlignmentY(0.0F);
        jLabelImage.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 51, 51), 1, true));
        jLabelImage.setMaximumSize(new java.awt.Dimension(20, 2));
        jLabelImage.setPreferredSize(new java.awt.Dimension(2, 90));
        
        jPanel.setBackground(new java.awt.Color(51, 153, 255));
        jPanel.setBorder(new EmptyBorder(0, 8, 0, 8));
        jPanel.setSize(getWidth(), CLOSE_PANEL_HEIGHT);
        
        jPanelRating.setBackground(new java.awt.Color(51, 153, 255));
        jPanelRating.setAlignmentX(0.0F);
        jPanelRating.setAlignmentY(0.0F);
        jPanelRating.setMaximumSize(new java.awt.Dimension(90, 45));
        jPanelRating.setOpaque(false);
        jPanelRating.setPreferredSize(new java.awt.Dimension(90, 45));
        jPanelRating.setLayout(new java.awt.BorderLayout());

        {
	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
	        this.setLayout(layout);
	
	        GroupLayout.SequentialGroup leftToRight = layout.createSequentialGroup();
	        leftToRight.addComponent(jLabelImage);
	        leftToRight.addComponent(jPanel);
	        
	        GroupLayout.SequentialGroup topToBottom = layout.createSequentialGroup();
	        GroupLayout.ParallelGroup row = layout.createParallelGroup();
	        row.addComponent(jLabelImage);
	        row.addComponent(jPanel);
	        topToBottom.addGroup(row);

	        leftToRight.addGap(15, 15, 15);
	        topToBottom.addGap(15, 15, 15);

	        layout.setHorizontalGroup(leftToRight);
	        layout.setVerticalGroup(topToBottom);
        }
        
        {
	        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(jPanel);
	        jPanel.setLayout(layout);
	
	        GroupLayout.SequentialGroup leftToRight = layout.createSequentialGroup();
	        GroupLayout.ParallelGroup column = layout.createParallelGroup();
	        column.addComponent(jLabelUserName);
	        column.addComponent(jPanelRating);
	        column.addComponent(jLabelComment);
	        leftToRight.addGroup(column);
	        
	        GroupLayout.SequentialGroup topToBottom = layout.createSequentialGroup();
	        topToBottom.addComponent(jLabelUserName);
	        topToBottom.addComponent(jPanelRating);
	        topToBottom.addComponent(jLabelComment);

	        leftToRight.addGap(15, 15, 15);
	        topToBottom.addGap(15, 15, 15);
	        
	        layout.setHorizontalGroup(leftToRight);
	        layout.setVerticalGroup(topToBottom);
        }
        
        DBAccessThread.invoke(getImageLoaderThread(jLabelImage));
        
        GUIUtils.setJComponentFontAndColor(jLabelUserName, 15);
        GUIUtils.setJComponentFontAndColorNonBold(jPanelRating, 15);
        GUIUtils.setJComponentFontAndColorNonBold(jLabelComment, 15);

        //placehold with empty picture:
        loadPlaceHoldPicture(jLabelImage);
                
        jLabelUserName.setText("<html><u>" + item.getUsername()
        		+ "</u> " + getAction() + " <i><u>" + getCommentedOn() + "</i></u> <font style=font-size:9px>("
        		+ DateDiffCalculator.getDiffString(item.getStartDate())
        		+ ")</font></html>");
        Rater rater = new Rater(item.getCommentRating(), -1);
        rater.setEnabled(false);
        jPanelRating.add(rater);
        jLabelComment.setText(item.getComment());
    }

    private javax.swing.JLabel jLabelImage;
    private javax.swing.JPanel jPanel;
    private javax.swing.JLabel jLabelUserName;
    private javax.swing.JPanel jPanelRating;
    private javax.swing.JLabel jLabelComment;
}
